package snowflake

import (
	"fmt"
	"gorm.io/gen"
	"gorm.io/gen/examples/dal"
	"strings"
)

func generate() {
	g := gen.NewGenerator(gen.Config{
		OutPath:           "../../dal/query",
		FieldWithIndexTag: true,
		FieldWithTypeTag:  true,
	})

	g.UseDB(dal.DB)
	g.WithFileNameStrategy(func(tableName string) string {
		tablePrefix := "t_"
		return strings.TrimPrefix(tableName, tablePrefix)
	})
	g.WithJSONTagNameStrategy(func(columnName string) (tagContent string) {
		prefix := "f_"
		return fmt.Sprintf(`%s`, strings.TrimPrefix(columnName, prefix))
	})

	// generate all table from database
	g.ApplyBasic(g.GenerateAllTable(gen.FieldTrimPrefix("f_"),
		gen.FieldTypeReg("^f_.*._id$", "snowflake.SFID"),
		gen.FieldType("f_deleted", "soft_delete.DeletedAt"),
		gen.FieldGORMTag("f_created", "autoCreateTime;column:f_created;not null"),
		gen.FieldGORMTag("f_updated", "autoUpdateTime;column:f_updated;not null"),
	)...)

	g.Execute()
}
